package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.serve.dto.MemberLoginDto;
import com.zzyl.serve.util.WechatUtil;
import com.zzyl.serve.vo.MemberLoginVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.FamilyMemberMapper;
import com.zzyl.serve.domain.FamilyMember;
import com.zzyl.serve.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author liuyp
 * @date 2025-08-21
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WechatUtil wechatUtil;
    @Autowired
    TokenService tokenService;
    private static final List<String> nickNamePool = List.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝");

    @Override
    public MemberLoginVo login(MemberLoginDto dto) {
        //微信小程序服务端返回的code，用于获取用户的账号信息
        String code = dto.getCode();
        //微信小程序服务端返回的phoneCode，用于获取用户的手机号信息
        String phoneCode = dto.getPhoneCode();
        //微信昵称
        String nickName = dto.getNickName();

        //获取openId和手机号
        String openId = wechatUtil.getOpenId(code);
        String phone = wechatUtil.getPhone(phoneCode);

        //根据openId查询数据库判断老人家属是否已经登录过
        LambdaQueryWrapper<FamilyMember> wrapper = Wrappers.<FamilyMember>lambdaQuery().eq(FamilyMember::getOpenId, openId);
        FamilyMember familyMember = familyMemberMapper.selectOne(wrapper);
            //如果登录过则执行新增操作,未登录过执行修改
        if(familyMember==null){
            familyMember = new FamilyMember();
            familyMember.setPhone(phone);
            familyMember.setName(nickNamePool.get((int)(Math.random()*(nickNamePool.size())))
                                    + phone.substring(7));
            familyMember.setOpenId(openId);
            save(familyMember);
        }else{
            //判断手机号是否跟数据库相同，不相同则执行修改
            if(!familyMember.getPhone().equals(phone)){
                familyMember.setPhone(phone);
                familyMemberMapper.updateById(familyMember);
            }
        }
        //生成token
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",familyMember.getId());
        claims.put("name",familyMember.getName());
        String token = tokenService.createToken(claims);
        //封装成对象返回给客户端
        MemberLoginVo memberLoginVo = new MemberLoginVo();
        memberLoginVo.setToken(token);
        memberLoginVo.setNickName(familyMember.getName());
        return memberLoginVo;
    }


}
